package cn.kgc.mes.storage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.storage.domain.WmIssueLine;
import cn.kgc.mes.storage.service.IWmIssueLineService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 生产领料单行Controller
 * 
 * @author jiyupo
 * @date 2025-09-20
 */
@RestController
@RequestMapping("/IssueLine/IssueLine")
public class WmIssueLineController extends BaseController
{
    @Autowired
    private IWmIssueLineService wmIssueLineService;

    /**
     * 查询生产领料单行列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WmIssueLine wmIssueLine)
    {
        startPage();
        List<WmIssueLine> list = wmIssueLineService.selectWmIssueLineList(wmIssueLine);
        return getDataTable(list);
    }

    /**
     * 导出生产领料单行列表
     */
    @Log(title = "生产领料单行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmIssueLine wmIssueLine)
    {
        List<WmIssueLine> list = wmIssueLineService.selectWmIssueLineList(wmIssueLine);
        ExcelUtil<WmIssueLine> util = new ExcelUtil<WmIssueLine>(WmIssueLine.class);
        util.exportExcel(response, list, "生产领料单行数据");
    }

    /**
     * 获取生产领料单行详细信息
     */
    @GetMapping(value = "/{lineId}")
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId)
    {
        return success(wmIssueLineService.selectWmIssueLineByLineId(lineId));
    }

    /**
     * 新增生产领料单行
     */
    @Log(title = "生产领料单行", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmIssueLine wmIssueLine)
    {
        return toAjax(wmIssueLineService.insertWmIssueLine(wmIssueLine));
    }

    /**
     * 修改生产领料单行
     */
    @Log(title = "生产领料单行", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmIssueLine wmIssueLine)
    {
        return toAjax(wmIssueLineService.updateWmIssueLine(wmIssueLine));
    }

    /**
     * 删除生产领料单行
     */
    @Log(title = "生产领料单行", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lineIds}")
    public AjaxResult remove(@PathVariable Long[] lineIds)
    {
        System.out.println("删除");
        return toAjax(wmIssueLineService.deleteWmIssueLineByLineIds(lineIds));
    }
//    根据id查询生产领料单头列表
    @GetMapping("checkLine")
    public Object selectWmIssueLineList(@RequestParam("issueId") Long issueId) {
      List<WmIssueLine> list = wmIssueLineService.selectWmIssueLineList2(issueId);
      Map<String, Object> map = new HashMap<>();
      if(list!=null){
          map.put("code",200);
          map.put("msg","查询成功");
          map.put("data",list);
      }else{
          map.put("code",0);
          map.put("msg","查询失败");
      }
      return map;
    }
    //根据issueId将表中对应的所有数据都删除
    @PostMapping("/deleLine")
    public Object deleteLine(@RequestBody Long[] issueId) {
        System.out.println(issueId);
        int i = wmIssueLineService.deleteWmIssueLineByIssueId(issueId);
        Map<String, Object> map = new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",0);
            map.put("msg","删除失败");
        }
        return map;
    }
}
